
const router = require('koa-router')()
const {
  getList,
  getDetail,
  newBuy,
  updateBuy, 
} = require('../controller/buy')

const { SuccessModel, ErrorModel } = require('../model/resModel')

router.prefix('/api/buy')

router.post('/doing', async (ctx, next) => { 
  const body = ctx.request.body
  const data = await getList(body)
  ctx.body = new SuccessModel(data)  
})
 
router.post('/done', async (ctx, next) => { 
   const body = ctx.request.body
   const data = await getList(body)
   ctx.body = new SuccessModel(data)  
 })
  
router.post('/add',  async function (ctx, next) {
  const body = ctx.request.body 
  const data = await newBuy(body)
  if (data){
	ctx.body = new SuccessModel(data,"添加购物车成功") 
  }else{
	ctx.body = new ErrorModel('添加购物车失败') 
  }
})
 
 router.post('/cancel', async (ctx, next) => { 
    const body = ctx.request.body
    const data = await updateBuy(body)
	if(data){
		ctx.body = new SuccessModel("作废成功")  
	}else{
		ctx.body = new SuccessModel("作废失败")  
	}
  })
  
 router.post('/commit', async (ctx, next) => { 
    const body = ctx.request.body
    const data = await updateBuy(body)
	if(data){
		ctx.body = new SuccessModel("结算成功")  
	}else{
		ctx.body = new SuccessModel("结算失败")  
	}
  })
  
router.post('/buy', async (ctx, next) => { 
     const body = ctx.request.body
     const data = await newBuy(body)
  	if(data){
  		ctx.body = new SuccessModel("购买成功")  
  	}else{
  		ctx.body = new SuccessModel("购买失败")  
  	}
   })
  
module.exports = router
